package at.horario.web.pages;

import at.horario.web.HorarioSession;
import at.horario.web.component.FieldComponentFeedbackPanel;
import at.horario.web.component.TooltipField;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import java.util.Iterator;

/**
 * Created by IntelliJ IDEA.
 * User: azarias.muchanga
 * Date: Dec 25, 2007
 * Time: 8:13:15 PM
 * To change this template use File | Settings | File Templates.
 */
public abstract class BaseForm extends Form {
    protected boolean showtooltiplabel;

    public boolean isShowtooltiplabel() {
        return showtooltiplabel;
    }

    public BaseForm(String id, IModel model) {
        super(id, model);
        final Label showtooltiplabelLabel = new Label("showtooltiplabelMsg", new Model("Mostrar/Esconder Tooltip"));
        showtooltiplabelLabel.setOutputMarkupId(true);

        final AjaxLink showtooltiplabelLink = new AjaxLink("showtooltiplabel", new Model()) {
            public void onClick(AjaxRequestTarget target) {
                showtooltiplabel = !showtooltiplabel;

                final Iterator iterator = BaseForm.this.iterator();
                while (iterator.hasNext()) {
                    Object o = iterator.next();
                    if (o instanceof TooltipField) {
                        target.addComponent((TooltipField) o);
                    }

                }
                target.addComponent(showtooltiplabelLabel);
            }
        };
        showtooltiplabelLink.add(showtooltiplabelLabel);
        add(showtooltiplabelLink);
        showtooltiplabelLink.setVisible(((HorarioSession) getSession()).contem("edittooltip"));

    }

    /**
     * Convenience method that adds and prepares a form component
     *
     * @param fc    form component
     * @param label IModel containing the string used in ${label} variable of
     *              validation messages
     */
    protected void add(FormComponent fc, IModel label, boolean tooltip) {
        // Add the component to the form
        add(fc);

        // Set its label model
        fc.setLabel(label);
        // Add feedback panel that will be used to display component errors
        final FieldComponentFeedbackPanel fieldComponent = new FieldComponentFeedbackPanel(fc.getId() + "-feedback", fc);
        fieldComponent.setOutputMarkupId(true);
        fieldComponent.setOutputMarkupPlaceholderTag(true);
        add(fieldComponent);
        if (tooltip)
            add(new TooltipField("_" + fc.getId()) {

                public boolean isLabelVisible() {
                    return showtooltiplabel;
                }
            });
    }

    protected void add(FormComponent fc, IModel label) {
        add(fc, label, true);
    }

}
